<template>
	<view class="pre-order">
		<view class="title">
			等待支付
		</view>

		<view class="address-holder" @tap="toChoiceAddress">
			<view v-if="!addressShow" class="placeholder">
				请选择地址
			</view>
			<view class="address">
				<view class="name">
					<text>姓名：{{address.name}}</text>
					<text>电话号：{{address.phoneNumber}}</text>
				</view>
				<view class="name">
					收货地址：{{address.city}} {{address.detailAddress}}
				</view>
			</view>
		</view>

		<view class="order-holder">
			<view class="order-info">
				<text>订单编号{{orderId}}</text>
				<text>等待支付</text>
			</view>
			<view class="list">
				<view class="item" v-for="item in orderList" :key="item.id">
					<image :src="item.productPic" mode=""></image>

					<view class="right">
						<view class="name">
							<text>{{item.productName}}</text>
							<text>￥{{item.productPrice}}</text>
						</view>

						<view class="count">
							<text>编号：{{item.productSkuCode}}</text>
							<text>x{{item.productQuantity}}</text>
						</view>

						<view class="type">
							<text>规格：</text>
							<text v-for="item1 in JSON.parse(item.productAttr)">{{item1.key}}:{{item1.value}} </text>
						</view>
					</view>
				</view>
			</view>

			<view class="sum-price">
				<view class="sum">
					合计:{{sumPrice}}元
				</view>
				<view class="submit-btn" @tap="toPayOrder">
					确认付款
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		requestOrderInfo,
		requestConfirmOrder
	} from 'api/user/order.js';
	export default {
		data() {
			return {
				address: '',
				orderId: '',
				addressShow: false,
				orderList: [],
				sumPrice: 0,
			};
		},
		methods: {
			toChoiceAddress() {
				uni.navigateTo({
					url: '/pages/user/address/address',
				})
			},
			getOrderInfo() {
				requestOrderInfo(this.orderId).then(res => {
					console.log(res);
					this.orderList = res.data.orderItems;
					this.sumPrice = res.data.orderBase.totalAmount;
				})
			},
			toPayOrder() {
				let obj = {
					addressId: this.address.id,
					orderId: this.orderId,
					orderItemList: this.orderList,
					payType: 0,
					sourceType: 0
				}
				
				requestConfirmOrder(obj).then(res=>{
					console.log(res);
					uni.setStorageSync('orderInfo',res.data);
					if(res.code === 20000){
						uni.showToast({
							title:'确定订单成功，1s后跳转到支付界面',
							icon:'success',
							duration:1000
						});
						
						setTimeout(()=>{
							uni.navigateTo({
								url:`/pages/user/cart/pay-order/pay-order?orderId=${res.data.orderId}`
							})
						},1000)
					}
				})
			}
		},
		onLoad(e) {
			this.orderId = e.orderId;
			this.getOrderInfo();
		},
		onShow() {
			if (uni.getStorageSync('address')) {
				this.addressShow = true;
				this.address = uni.getStorageSync('address');
			}
		},
	}
</script>

<style lang="scss" scoped>
	.pre-order {
		box-sizing: border-box;
		padding: 0 40rpx;
		width: 100%;

		.title {
			font-size: 36rpx;
			color: #494949;
			height: 88rpx;
			border-bottom: 2rpx solid #ccc;
			line-height: 88rpx;
		}

		.address-holder {
			box-sizing: border-box;
			padding: 30rpx 0;
			color: #494949;
			border-bottom: 2rpx solid #ccc;
		}

		.order-holder {
			box-sizing: border-box;
			margin: 30rpx 0;
			padding: 36rpx;
			border-radius: 24rpx;
			background-color: #fff;
			overflow: hidden;

			.order-info {
				display: flex;
				padding-bottom: 20rpx;
				justify-content: space-between;
				font-size: 24rpx;
				border-bottom: 1px solid #f1ece7;
			}

			.list {
				.item {
					box-sizing: border-box;
					padding: 30rpx 0;
					display: flex;
					align-items: center;
					width: 100%;
					border-bottom: 1px solid #f1ece7;

					image {
						width: 162rpx;
						height: 162rpx;
					}

					.right {
						margin-left: 30rpx;

						.name {
							font-size: 13px;
							line-height: 37px;
						}

						.count {
							display: flex;
							justify-content: space-between;
							font-size: 22rpx;
							color: #8d8d8d;
							line-height: 30rpx;
							margin-top: 4rpx;
						}
					}
				}
			}

			.sum-price {
				float: right;
				margin-top: 20rpx;

				.submit-btn {
					margin-left: 32rpx;
					margin-top: 20rpx;
					float: right;
					width: 124rpx;
					height: 50rpx;
					font-size: 26rpx;
					border: 2rpx solid #2d4f43;
					text-align: center;
					color: #2d4f43;
					border-radius: 10rpx;
					line-height: 50rpx;
				}
			}


		}


	}
</style>
